package com.blog.controller.admin;

import com.blog.dto.CategoryDTO;
import com.blog.dto.CategorySearchDTO;
import com.blog.result.Result;
import com.blog.service.CategoryService;
import com.blog.vo.CategoryVO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController("adminCategory")
@RequestMapping("/admin/category")
@Slf4j
public class CategoryController {

    @Autowired
    private CategoryService categoryService;

    @GetMapping("/list")
    public Result<List<CategoryVO>> ListCategory(){
        List<CategoryVO> list = categoryService.list();
        return Result.success(list);
    }

    @GetMapping("/listByUId/{id}")
    public Result<List<CategoryVO>> ListByUIdCategory(@PathVariable("id") Long Uid){
        log.info("根据用户ID查询类别：{}",Uid);
        List<CategoryVO> list = categoryService.listByUId(Uid);
        return Result.success(list);
    }

    @GetMapping("/listById/{id}")
    public Result<CategoryVO> listByUIdCategory(@PathVariable("id") Long id){
        log.info("根据ID查询类别：{}",id);
        CategoryVO categoryVO = categoryService.listById(id);
        return Result.success(categoryVO);
    }


    @PostMapping("/listByName")
    public Result<List<CategoryVO>> ListByIdAndName(@RequestBody CategorySearchDTO categorySearchDTO){
        log.info("根据用户ID和类别名称模糊查询类别：{}",categorySearchDTO);
        List<CategoryVO> list = categoryService.SearchCategory(categorySearchDTO);
        return Result.success(list);
    }


    @PostMapping("/AddCategory/{value}")
    public Result Addategory(@PathVariable("value") String value){
        log.info("添加类别：{}",value);
        boolean b = categoryService.AddCategory(value);
        if (!b)return Result.fail();
        return Result.success();
    }

    @PutMapping("/EditCategory")
    public Result EditCategory(@RequestBody CategoryDTO categoryDTO){
        log.info("修改类别：{}",categoryDTO);
        boolean b = categoryService.EditCategory(categoryDTO);
        if (!b)return Result.fail();
        return Result.success();
    }

    @DeleteMapping("/DeleteCategory/{id}")
    public Result EditCategory(@PathVariable("id") Long id){
        log.info("删除类别：{}",id);
        boolean b = categoryService.DeleteCategory(id);
        if (!b)return Result.fail();
        return Result.success();
    }
}
